<?php


namespace App\Models\CacheModel;


use app\common\lib\exception\ApiException;
use App\Models\Api\BaseCache;
use think\Exception;
use AstroTool\Cache\CacheManage;

class ZlVip extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl_vip';

    protected $cacheModel = 'vip.vip_level';

    /**
     * 获取所有的VIP等级信息
     * @param array $field
     * @return array|mixed
     * @throws \Exception
     */
    public function getLevelAll($field = [])
    {
        $data = CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel, 0)
            ->setPage(-1)
            ->setSort('asc')
            ->setListKeyType(0)// 类型 用于分页 例如 0全部 1待支付 2已支付 //
            ->getListPage(function ($ids = [], $isCache = true, $sortType = 'desc')
            {
                if ($isCache)
                { // 开启了缓存
                    return $this->getInfoByIdsInData($ids, [], $sortType);
                } else
                { // 没开启缓存
                    return $this->getPaginateInData([], $sortType);
                }
            }, 'sort', $field, 0);

        return $data['data'];
    }

    /**
     * 重写数据时获取数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            $where = [];
            if ($ids)
            {
                $where['level'] = ['in', $ids];
            }

            $data = $this->where($where)->where('status', 1)->order("sort {$sortType}")->group('level')->column('id,name,level,star_num,grow_num,status,sort,create_time,update_time', 'level');
        }
        catch (Exception $e)
        {
            $data = [];
        }

        return $data;
    }

    /**
     * 未开启缓存从数据库获取分页数据
     * @param array $type 分页数据状态
     * @param string $sortType 排序方式
     * @return array
     */
    public function getPaginateInData($type, $sortType)
    {
        try
        {
            $data = $this->where('status', 1)->order("sort {$sortType}")->group('level')->field('id,name,level,star_num,grow_num,status,sort,create_time,update_time')->paginate()->toArray();
        }
        catch (Exception $e)
        {
            $data = [];
        }

        return $data;
    }
}
